<template>
  <div>
    <el-button @click="dialogVisible=true">添加分类</el-button>
    <el-table :data="lostFoundCategoryList" style="width: 100%">
      <el-table-column prop="lostFoundCategoryId" label="分类ID" width="180"></el-table-column>
      <el-table-column prop="name" label="分类名称" width="180"></el-table-column>
      <el-table-column prop="description" label="说明"></el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="scope">
          <el-button size="small" @click="handleEdit( scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row.lostFoundCategoryId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="400">
      <el-form :model="form" label-width="80">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="说明">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {onMounted, reactive, ref} from "vue";
import {
  reqAddLostFoundCategory,
  reqDeleteLostFoundCategory,
  reqGetLostFoundCategoryList,
  reqUpdateLostFoundCategory
} from "@/api/serve/lost";

let lostFoundCategoryList = ref([])
let form = reactive({
  name: '',
  description: ''
})
let dialogTitle = ref('添加分类')
let dialogVisible = ref(false)

let getLostFoundCategoryList = async () => {
  let res = await reqGetLostFoundCategoryList()
  lostFoundCategoryList.value = res.data
}
let submitForm = () => {
  if (dialogTitle.value != "添加分类") {
    reqUpdateLostFoundCategory(form).then(() => {
      resetForm()
      getLostFoundCategoryList()
      dialogVisible.value = false
    })
  } else {
    reqAddLostFoundCategory(form).then(() => {
      resetForm()
      getLostFoundCategoryList()
      dialogVisible.value = false
    })
  }
}
let resetForm = () => {
  form.name = ''
  form.description = ''
}
let handleEdit = (row: any) => {
  dialogTitle.value = '编辑分类'
  dialogVisible.value = true
  form.name = row.name
  form.description = row.description
}
let handleDelete = (lostFoundCategoryId) => {
  reqDeleteLostFoundCategory(lostFoundCategoryId).then(() => {
    getLostFoundCategoryList()
  })
}
onMounted(() => {
  getLostFoundCategoryList()
})
</script>

<style scoped>

</style>
